//
//  MYGMedicalToolsGRACEBeforeView.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/5/27.
//

import UIKit
import JXSegmentedView
class MYGMedicalToolsGRACEBeforeView: UIView {
    // MARK: - Property
    private var age: Int?
    private var heart: Int?
    private var pressure: Int?
    private var creatinine: Int?
    private var killip: Int?
    private var risk: Int?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        backgroundColor = .white
        addSubviews([
            scrollView,
            commitButton
        ])
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(commitButton.snp.top)
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let line1 = createSeparator()
        let line2 = createSeparator()
        let line3 = createSeparator()
        let line4 = createSeparator()
        let line5 = createSeparator()
        let line6 = createSeparator()

        scrollContentView.addSubviews([
            ageInputView,
            line1,
            heartView,
            line2,
            pressureView,
            line3,
            creatinineView,
            line4,
            killipView,
            line5,
            riskView,
            line6,
            scoreView
        ])
        
        ageInputView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(ageInputView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        heartView.snp.makeConstraints { make in
            make.top.equalTo(ageInputView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(heartView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        pressureView.snp.makeConstraints { make in
            make.top.equalTo(heartView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line3.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(pressureView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        creatinineView.snp.makeConstraints { make in
            make.top.equalTo(pressureView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line4.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(creatinineView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        killipView.snp.makeConstraints { make in
            make.top.equalTo(creatinineView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line5.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(killipView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        riskView.snp.makeConstraints { make in
            make.top.equalTo(killipView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line6.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(riskView.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(riskView.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()

    private lazy var scrollContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var ageInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .age, frame: .zero)
        view.inputDidChange = {[weak self] (age) in
            self?.age = Int(age)
        }
        return view
    }()
    
    private lazy var heartView: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "心率（次/min）"
        view.items = ["<50", "50-69", "70-89", "90-109", "110-149", "150-199","≥200"]
        view.action = {[weak self] (index) in
            self?.heart = index
        }
        return view
    }()
    
    private lazy var pressureView: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "收缩压（mmHg）"
        view.items = ["<80", "80-99", "100-119", "120-139", "140-159", "160-199", "≥200"]
        view.action = {[weak self] (index) in
            self?.pressure = index
        }
        return view
    }()
    
    private lazy var creatinineView: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "肌酐（mg/dL）"
        view.items = ["0-0.39", "0.4-0.79", "0.8-1.19", "1.2-1.59", "1.6-1.99", "2.0-3.99", "≥4"]
        view.action = {[weak self] (index) in
            self?.creatinine = index
        }
        return view
    }()
    
    private lazy var killipView: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "Killip分级"
        view.items = ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ"]
        view.action = {[weak self] (index) in
            self?.killip = index
        }
        return view
    }()
    
    private lazy var riskView: MYGMedicalToolsSheetChooseView = {
        let view = MYGMedicalToolsSheetChooseView()
        view.title = "危险因素"
        view.items = ["无","入院时心脏骤停", "心电图ST段改变", "心肌坏死标志升高"]
        view.action = {[weak self] (index) in
            self?.risk = index
        }
        return view
    }()
    
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "正常"
        view.title = "0"
        return view
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
}


extension MYGMedicalToolsGRACEBeforeView: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        self
    }
}

extension MYGMedicalToolsGRACEBeforeView {
    @objc func commit() {
        guard let age = age, age > 0 else {
            MMYGHUD.showToast(text: "请输入正确的年龄")
            return
        }
        
        guard let heart = heart else {
            MMYGHUD.showToast(text: "请选择心率")
            return
        }
        
        guard let pressure = pressure else {
            MMYGHUD.showToast(text: "请选择收缩压")
            return
        }
        
        guard let creatinine = creatinine else {
            MMYGHUD.showToast(text: "请选择肌酐")
            return
        }
        
        guard let killip = killip else {
            MMYGHUD.showToast(text: "请选择Killip分级")
            return
        }
        
        guard let risk = risk else {
            MMYGHUD.showToast(text: "请选择危险因素")
            return
        }
        
        
        var score = 0
        var ageScore = 0
        if age < 30 {
            ageScore = 0
        } else if age <= 39 {
            ageScore = 8
        } else if age <= 49 {
            ageScore = 25
        } else if age <= 59 {
            ageScore = 41
        } else if age <= 69 {
            ageScore = 58
        } else if age <= 79 {
            ageScore = 75
        } else if age <= 89 {
            ageScore = 91
        } else {
            ageScore = 100
        }
            
        var heartScore = 0
        if heart == 0 {
            heartScore = 0
        } else if heart == 1 {
            heartScore = 3
        } else if heart == 2 {
            heartScore = 9
        } else if heart == 3 {
            heartScore = 15
        } else if heart == 4 {
            heartScore = 24
        } else if heart == 5 {
            heartScore = 38
        } else if heart == 6 {
            heartScore = 46
        }
        
        var pressureScore = 0
        if pressure == 0 {
            pressureScore = 58
        } else if pressure == 1 {
            pressureScore = 53
        } else if pressure == 2 {
            pressureScore = 43
        } else if pressure == 3 {
            pressureScore = 34
        } else if pressure == 4 {
            pressureScore = 24
        } else if pressure == 5 {
            pressureScore = 10
        } else if pressure == 6 {
            pressureScore = 0
        }
        
        
        var creatinineScore = 0
        if creatinine == 0 {
            creatinineScore = 1
        } else if creatinine == 1 {
            creatinineScore = 4
        } else if creatinine == 2 {
            creatinineScore = 7
        } else if creatinine == 3 {
            creatinineScore = 10
        } else if creatinine == 4 {
            creatinineScore = 13
        } else if creatinine == 5 {
            creatinineScore = 21
        } else if creatinine == 6 {
            creatinineScore = 28
        }
        
        var killipScore = 0
        if killip == 0 {
            killipScore = 0
        } else if killip == 1 {
            killipScore = 20
        } else if killip == 2 {
            killipScore = 39
        } else if killip == 3 {
            killipScore = 59
        }
        
        var riskScore = 0
        if risk == 0 {
            riskScore = 0
        } else if risk == 1 {
            riskScore = 39
        } else if risk == 2 {
            riskScore = 28
        } else if risk == 3 {
            riskScore = 14
        }
        
        score = ageScore + heartScore + pressureScore + creatinineScore + killipScore + riskScore
        if score <= 108 {
            scoreView.level = "低危"
            scoreView.progress =   (1.0 / 2.0) * (Double(score) / 108.0)
        } else if score <= 140 {
            scoreView.level = "中危"
            scoreView.progress = ((1.0 / 2.0) * (Double(score - 108) / Double(140-108))) + 0.5
        } else {
            scoreView.level = "高危"
            scoreView.progress = 1
        }
        scoreView.title = "\(score)"
    }
}
